﻿package com.pool.core 
{
	import SpLib.Com.SpLibEvent;
	import SpLib.Net.RPC;
	
	import com.pool.data.RoomMgr;
	import com.pool.data.ServerMgr;
	import com.pool.events.RoomEvent;
	
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	
	import org.dm.display.DisplayMgr;
	
	/**
	 * ...
	 * @author Scott
	 */
	public class Rooms extends EventDispatcher
	{
		private var _display:Sprite;
		private var _rooms:Array;
		private var _roomsGray:Array;
		private var _roomsList:Array;
		private var _roomIn:SimpleButton;
		private var _returnBtn:SimpleButton;
		public var data:Array;
		
		private var _roomKeyList:Array;
		private var _roomPoint:int = 0;
		
		public function Rooms() 
		{
			super();
			_rooms = new Array();
			_roomsGray = new Array();
			_roomsList = new Array();
			_roomKeyList = new Array();
			
			_display = DisplayMgr.getDisplay("rooms.bg","win.ServerWin");
			
			_rooms.push(_display.getChildByName("Rooms1") as Sprite);
			_rooms.push(_display.getChildByName("Rooms2") as Sprite);
			_rooms.push(_display.getChildByName("Rooms3") as Sprite);
			_rooms.push(_display.getChildByName("Rooms4") as Sprite);
			
			_roomsGray.push(_display.getChildByName("Rooms1gray") as Sprite);
			_roomsGray.push(_display.getChildByName("Rooms2gray") as Sprite);
			_roomsGray.push(_display.getChildByName("Rooms3gray") as Sprite);
			_roomsGray.push(_display.getChildByName("Rooms4gray") as Sprite);
			
			_roomsList.push(_display.getChildByName("RoomList1") as Sprite);
			_roomsList.push(_display.getChildByName("RoomList2") as Sprite);
			_roomsList.push(_display.getChildByName("RoomList3") as Sprite);
			_roomsList.push(_display.getChildByName("RoomList4") as Sprite);
			
			_roomIn = _display.getChildByName("JoinRoom") as SimpleButton;
			
			//设置返回按钮
			_returnBtn = DisplayMgr.getDisplay("rooms.return", "btn.ReturnBtn");
			_returnBtn.x = _display.width - 30;
			_returnBtn.y = 5;
			_display.addChild(_returnBtn);
		}
		
		public function init():void 
		{
			for (var i:int = 0; i < 4; i++) 
			{
				_display.removeChild(_rooms[i]);
				_roomsGray[i].addEventListener(MouseEvent.ROLL_OVER, onMoveOn1);
				_roomsList[i].addEventListener(MouseEvent.ROLL_OVER, onMoveOn2);
			}
			
			RPC.Call("Qin::房间管理", "GetRoomList", ["台球16球"], onRoomListOK, onRoomListErr);
			
		}
		
		//鼠标在上部的事件
		private function onMoveOn1(e:MouseEvent):void 
		{
			for (var i:int = 0; i < 4; i++) 
			{
				if (_roomsGray[i] == e.target) break;
			}
			_display.addChild(_rooms[i]);
			_rooms[i].addEventListener(MouseEvent.ROLL_OUT, onMoveOut1);
		}
		
		private function onMoveOut1(e:MouseEvent):void 
		{
			for (var i:int = 0; i < 4; i++) 
			{
				if (_rooms[i] == e.target) break;
			}
			_rooms[i].removeEventListener(MouseEvent.ROLL_OUT, onMoveOut1);
			_display.removeChild(_rooms[i]);
		}
		
		//鼠标在下部的事件
		private function onMoveOn2(e:MouseEvent):void 
		{
			for (var i:int = 0; i < 4; i++) 
			{
				if (_roomsList[i] == e.target) break;
			}
			_display.addChild(_rooms[i]);
			_roomsList[i].addEventListener(MouseEvent.ROLL_OUT, onMoveOut2);
		}
		
		private function onMoveOut2(e:MouseEvent):void 
		{
			for (var i:int = 0; i < 4; i++) 
			{
				if (_roomsList[i] == e.target) break;
			}
			_roomsList[i].removeEventListener(MouseEvent.ROLL_OUT, onMoveOut2);
			_display.removeChild(_rooms[i]);
		}
		
		public function onEnterRoom(e:MouseEvent):void 
		{
			RoomMgr.Instance.roomLink = _roomKeyList[0][1].split("@")[1];
			RoomMgr.Instance.roomNo = _roomKeyList[0][0];
			
			dispatchEvent(new RoomEvent(RoomEvent.LOGINROOM));
		}
		
		private function onRoomListOK(e:SpLibEvent):void 
		{
			for each(var tt:Array in e.m_Agv)
			{
				com.pool.data.ServerMgr.Instance.addAddress(tt[1].split("@")[1],tt[0]);
			}
			_roomKeyList = e.m_Agv;
			_roomsGray[0].addEventListener(MouseEvent.CLICK, onLoginRoom);
			_roomIn.addEventListener(MouseEvent.CLICK, onEnterRoom);
		}
		
		private function onRoomListErr(e:SpLibEvent):void 
		{
			trace("121313");
			//dispatchEvent(new RoomEvent(SpLibEvent.RPC_RETERR));
		}
		
		public function onLoginRoom(e:MouseEvent):void 
		{
			dispatchEvent(new RoomEvent(RoomEvent.LOGINROOM));
		}
		
		public function get display():Sprite
		{
			return this._display; 
		}
	}

}